package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.core.framework.dao.mybatis.paginator.domain.PageBounds;
import com.jiyinit.web.core.business.dto.RolePageRequest;
import com.jiyinit.web.core.business.entity.TabCoreRoleEntity;
import com.jiyinit.web.core.common.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lishaochuan
 * @version 1.0
 * @date 2017/4/19 17:02
 * @since 1.0
 */
@Repository("core.tabCoreRoleDao")
public class TabCoreRoleDao {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(TabCoreRoleDao.class);

    private String SQLID = "core.tabCoreRoleDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;


    /**
     * 根据角色ID查询角色
     * @author lishaochuan
     * @create 2017/4/20 11:32
     * @param
     * @return
     */
    public TabCoreRoleEntity selectRoleById(int roleId){
        return mybatisDaoContext.findOne(SQLID + "selectRoleById", TabCoreRoleEntity.class, roleId);
    }

    /**
     * 分页查询角色
     * @author lishaochuan
     * @create 2017/4/19 17:31
     * @param
     * @return
     */
    public PagingResult<TabCoreRoleEntity> selectRoleForPage(RolePageRequest rolePageRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(rolePageRequest.getLimit());
        pageBounds.setPage(rolePageRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "selectRoleForPage", TabCoreRoleEntity.class, rolePageRequest, pageBounds);
    }

    /**
     * 查询所有角色
     * @author zhaolaiwei
     * @create 2017/4/120 09:28
     * @return
     */
    public List<TabCoreRoleEntity> selectAll(){
        return mybatisDaoContext.findAll(SQLID + "selectAll");
    }


    /**
     * 保存角色
     * @author lishaochuan
     * @create 2017/4/20 11:04
     * @param
     * @return
     */
    public int save(TabCoreRoleEntity tabCoreRoleEntity){
        return mybatisDaoContext.save(SQLID + "save", tabCoreRoleEntity);
    }

    /**
     * 修改角色
     * @author lishaochuan
     * @create 2017/4/20 11:43
     * @param
     * @return
     */
    public int update(TabCoreRoleEntity tabCoreRoleEntity){
        return mybatisDaoContext.update(SQLID + "update", tabCoreRoleEntity);
    }


    /**
     * 逻辑删除角色
     * @author lishaochuan
     * @create 2017/4/20 14:05
     * @param
     * @return
     */
    public int deleteByRoleIds(Integer[] roleIds){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roleIds", roleIds);
        map.put("lastModifyId", UserUtil.getCurrentUser().getUserId());
        map.put("lastModifyTime",new Date());
        return mybatisDaoContext.update(SQLID + "deleteByRoleIds", map);
    }
}
